package string;

import java.util.Scanner;

/*
键盘录入字符串，长度小于等于9，只能是数字，将内容变成罗马数字
 */
public class Str14 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str;
        while (true){
            System.out.println("请输入一个字符串：");
             str = sc.next();
            //校验字符串是否满足规则
            boolean flag=checkStr(str);
            if(flag){
                break;
            }else {
                System.out.println("当前字符串不符合规则，重新录入");
                continue;
            }
        }
        //将内容变成罗马数字  查表法
        StringBuilder sb=new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            int number = c - '0';
            String s = changeLuoMa(number);
            sb.append(s);
        }
        System.out.println(sb);

    }

    //查表法
    public static String changeLuoMa(int number){
        String[] arr={"","I","II","III","IV","V","VI","VII","VIII","IX"};
        return arr[number];
    }
    //校验字符串是否满足规则
    public static boolean checkStr(String str){
        if(str.length() > 9){
            return false;
        }
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if(c < '0' || c > '9'){
                return false;
            }
        }
        return true;
    }

}
